<?php

namespace App\Controller\Home;

use App\Models\Conn;
use App\Models\UsersModel;
use Core\Result\Result;

class Query
{
    /**
     * 查询旗下账号
     * */
    public function getTeam($data,$int=1,$total=0){
        $id = $data["username"];
        $pid = $data;
        $conn = new Conn();
        if($int == 1){
            echo "================================第".$int."层================================<br/>";
            $sql="select `id`,`username`,`pid`,`capping`,`sach`,`pid`,`peer_reward`,`share_rewards` from `users` where `username` = '$id'";
            $readConn = $conn->readConn($sql);
            echo ("ID：".$readConn["id"].",账号：".$readConn["username"].",上级ID：".$readConn["pid"].",封顶值：".$readConn["capping"].",业绩：".$readConn["sach"].",销售奖励：".$readConn["peer_reward"]."<br/>");
            $pid = $readConn["id"];
        }
        $userAllMember = UsersModel::getUserAllPid($conn, $pid);
        $int ++;
        $pid ='';
        $level=0;
        echo "================================第".$int."层================================<br/>";
        foreach ($userAllMember as $value){
            echo ("ID：".$value["id"].",账号：".$value["username"].",上级ID：".$value["pid"].",封顶值：".$value["capping"].",业绩：".$value["sach"].",销售奖励：".$value["peer_reward"]."<br/>");
            $pid .=$value["id"].",";
            $total++;
            $level++;
        }
        echo "第：".$int."层,总计人数：".$level."<br/>";
        if(!empty($pid)){
            $pid = rtrim($pid,",");
            self::getTeam($pid,$int,$total);
        }
        echo "总计人数：".$total."<br/>";
        Result::success("查询完毕");
    }
    /**
     * 查询旗下账号
     * */
    public function getTeamDesc($data){
        $id = $data["username"];
        $conn = new Conn();
        $sql="select `id`,`username`,`truename`,`pid`,`capping`,`sach`,`peer_reward`,`share_rewards` from `users` where `username` = '$id'";
        $readConn = $conn->readConn($sql);
        $userSuperiorAllSid = UsersModel::getUserSuperiorAllSid($conn, $readConn["id"]);
        $total = 0;
        foreach ($userSuperiorAllSid as $i=> $value){
            echo ("层：".$i.",ID：".$value["id"].",账号：".$value["username"].",姓名：".$value["truename"].",上级ID：".$value["pid"].",封顶值：".$value["capping"].",业绩：".$value["sach"].",销售奖励：".$value["peer_reward"]."<br/>");
            $total++;
        }
        echo "总计人数：".$total."<br/>";
        Result::success("查询完毕");
    }
}